import asyncio

import httpx

# 视频BV号列表
bvid_list = [
    "BV1sduCzTE5j",
    "BV1qWtmzJE4i",
    "BV1DRt2zuEbU",
    "BV1Kz8cz6E94",
    "BV1PStfzwEmQ",
    "BV1Rttmz2EXg",
    "BV12XghzCEym",
    "BV14R8XzHE7K",
    "BV1J6hWzwEhS",
    "BV1YB8TzvEgg",
    "BV13Nghz2EyH",
    "BV1dWgezcEme",
    "BV1xbb3zPEHZ",
    "BV1nZb9z5EJb",
    "BV16GutznEwg",
    "BV1S7b8zWEYQ",
    "BV1hvtwzLEFj",
    "BV1HLuEztE7z",
    "BV1e7bVzJEg9",
    "BV1jxunzbEe6",
    "BV1ukbyzeEgf",
    "BV1sC8WzKEM2",
    "BV1zBhHzREwi",
    "BV1aqbdziE7m",
    "BV15BbvzLEd7",
    "BV1vk8wz4ENP",
    "BV1vQuyzZEC9",
    "BV1MEubzKEJ2",
    "BV1oj8gzjE4e",
    "BV1kXuDzHEe5",
    "BV143ghzkEt9",
    "BV1sog7zeEHs",
    "BV1C48ezoEuF",
    "BV1SLuQziEay",
    "BV1JBu7zXEud",
    "BV13WtXzRERB",
    "BV1EDucz6Epv",
    "BV15TubzCEek",
    "BV1qntRzvENb",
    "BV1oQtjzfEp3",
    "BV1QMhJzqEbA",
    "BV1J2bVz8EZs",
    "BV1V1hJzGEBR",
    "BV1REuxzBEHW",
    "BV1XUtgzqEeT",
    "BV1LA8ozkEas",
    "BV1M14RzsEDD",
    "BV1GB8TzvET6",
    "BV133gczcEKZ",
    "BV11mtizPEe6",
    "BV1cctzzPE7x",
    "BV1WCgpzUEen",
    "BV1R4unzoEfG",
    "BV1W8bCzXEWu",
    "BV1kN8HzWE62",
    "BV1Js8rzGEtN",
    "BV1hGgWzZEME",
    "BV1gZbCzeEre",
    "BV186tszgEVW",
    "BV1H1bPzgEmL",
    "BV1MT8TzoEYM",
    "BV1TY8kzdEXf",
    "BV1TXbqzCEW6",
    "BV1N8bRzpE3P",
    "BV1xVtbz5EKc",
    "BV1pRgizmEMh",
    "BV1mstxzuEMA",
    "BV13QtgziE48",
    "BV19ubazBEf1",
    "BV1C6bPzkEmU",
    "BV12attzGELM",
    "BV1C4b8z7EUY",
    "BV1Fb8FzbE3q",
    "BV1Pk8YzZEfo",
    "BV1Nkg3zSEHz",
    "BV1gLgAzKEgN",
    "BV1fpgbzxE6u",
    "BV12UbyzbEjA",
    "BV1dWu8zDEKi",
    "BV1k4t9zrEzb",
    "BV1ea8tzFELY",
    "BV1wM8GzhEZY",
    "BV14E4fzbESH",
    "BV11ehAz1ExS",
    "BV16b8azmEjK",
    "BV1xe89z9Ehb",
    "BV16Yb8zVEvQ",
    "BV1xtbnzdEpv",
    "BV1RjgEzSEhZ",
    "BV1xkt1zbE3W",
    "BV1p7tbzrEJL",
    "BV1AP8gzxEYs",
    "BV1RabWzSEDu",
    "BV1dz8RzuEvk",
    "BV1FNhAziEUA",
    "BV1JLbhzyE6U",
    "BV16StazjEHp",
    "BV1Psu4zWECB",
    "BV1pMt3zREgp",
    "BV1PYtqzeE6b",
]

# 全局的请求头，模拟Edge浏览器访问
headers = {
    "accept": "application/json, text/plain, */*",
    "accept-encoding": "gzip, deflate, br, zstd",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "origin": "https://www.bilibili.com",
    "priority": "u=1, i",
    "referer": "https://www.bilibili.com/",
    "sec-ch-ua": '"Chromium";v="140", "Not=A?Brand";v="24", "Microsoft Edge";v="140"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"',
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0",
}


async def fetch_duration(bvid, client):
    """获取单个视频的时长"""
    url = "https://api.bilibili.com/x/web-interface/view"
    params = {"bvid": bvid}

    try:
        response = await client.get(url, params=params, headers=headers)
        response.raise_for_status()  # 检查HTTP错误状态

        data = response.json()

        # 检查API返回的状态码
        if data.get("code") != 0:
            print(f"获取 {bvid} 失败: {data.get('message', '未知错误')}")
            return None

        # 返回时长（秒）
        return data.get("data", {}).get("duration")

    except Exception as e:
        print(f"请求 {bvid} 时发生错误: {str(e)}")
        return None


async def main():
    """主函数，批量获取视频时长"""
    # 创建异步客户端
    async with httpx.AsyncClient(timeout=10.0) as client:
        # 并发获取所有视频的时长
        tasks = [fetch_duration(bvid, client) for bvid in bvid_list]
        durations = await asyncio.gather(*tasks)

        # 按行输出每个视频的时长
        for bvid, duration in zip(bvid_list, durations):
            if duration is not None:
                print(f"{bvid}: {duration}秒")


if __name__ == "__main__":
    asyncio.run(main())
